//
//  XMHeightChooseView.m
//  XunMi
//
//  Created by MacBook on 2024/2/29.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMHeightChooseView.h"
#import "RulerView.h"

@interface XMHeightChooseView () <RulerViewDelegate>

@property (weak, nonatomic) IBOutlet UIView *hBgV;
@property (weak, nonatomic) IBOutlet UILabel *hLa;

@property (weak, nonatomic) IBOutlet UIView *wBgv;
@property (weak, nonatomic) IBOutlet UILabel *wLa;

@property (nonatomic, copy) NSString *hStr;
@property (nonatomic, copy) NSString *wStr;
@property (nonatomic, strong) RulerView *hRulerView;                 //数字居下
@property (nonatomic, strong) RulerView *wRulerView;                 //数字居下

@end

@implementation XMHeightChooseView

- (void)awakeFromNib {
    [super awakeFromNib];
    [self.hBgV addSubview:self.hRulerView];
    [self.hRulerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.hBgV);
    }];
    
    [self.wBgv addSubview:self.wRulerView];
    [self.wRulerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.wBgv);
    }];
}

- (IBAction)cacelAct:(UIButton *)sender {
    if (self.cancelBlock) {
        self.cancelBlock();
    }
}

- (IBAction)sureAct:(UIButton *)sender {
    if (self.sureBlock) {
        self.sureBlock(self.hStr, self.wStr);
    }
}


#pragma mark - 代理方法
- (void)rulerSelectValue:(double)value tag:(NSInteger)tag {
    if (tag == 1) {
        self.hStr = kStringFormat(@"%licm",(long) value);
        self.hLa.text = [NSString stringWithFormat:@"身高：%licm", (long)value];
    } else if (tag == 2) {
        self.wStr = kStringFormat(@"%likg",(long) value);
        self.wLa.text = [NSString stringWithFormat:@"体重：%likg", (long)value];
    }
    
    NSLog(@"value = %lf", value);
}

#pragma mark - getter

- (RulerView *)hRulerView {
    if (!_hRulerView) {
        _hRulerView = [[RulerView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 60)];
        _hRulerView.backgroundColor = UIColor.whiteColor;
        _hRulerView.tag = 1;
        _hRulerView.delegate = self;
        
        RulerConfig *config = [[RulerConfig alloc] init];
        //刻度高度
        config.shortScaleLength = 22;
        config.longScaleLength = 22;
        //刻度宽度
        config.scaleWidth = 1;
        //刻度起始位置
        config.shortScaleStart = 4;
        config.longScaleStart = 4;
        //刻度颜色
        config.scaleColor = kHEX(@"#959595");
        //刻度之间的距离
        config.distanceBetweenScale = 15;
        //刻度距离数字的距离
        config.distanceFromScaleToNumber = 8;
        //指示视图属性设置
        config.pointSize = CGSizeMake(1.5, 30);
        config.pointColor = k1F1F1F;
        config.pointStart = 0;
        //文字属性
        config.numberFont = [UIFont systemFontOfSize:14];
        config.numberColor = k1F1F1F;
        //数字所在位置方向
        config.numberDirection = numberBottom;
        
        //取值范围
        config.max = 300;
        config.min = 100;
        //默认值
        config.defaultNumber = [XMUtil isBlankString:kUserModel.height] ? 170 : kUserModel.height.doubleValue;
        config.infiniteLoop = YES;
        config.offset = 1;
//        config.selectionEnable = YES;
        _hRulerView.rulerConfig = config;
    }
    return _hRulerView;
}

- (RulerView *)wRulerView {
    if (!_wRulerView) {
        _wRulerView = [[RulerView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 60)];
        _wRulerView.backgroundColor = UIColor.whiteColor;
        _wRulerView.tag = 2;
        _wRulerView.delegate = self;
        
        RulerConfig *config = [[RulerConfig alloc] init];
        //刻度高度
        config.shortScaleLength = 22;
        config.longScaleLength = 22;
        //刻度宽度
        config.scaleWidth = 1;
        //刻度起始位置
        config.shortScaleStart = 4;
        config.longScaleStart = 4;
        //刻度颜色
        config.scaleColor = kHEX(@"#959595");
        //刻度之间的距离
        config.distanceBetweenScale = 15;
        //刻度距离数字的距离
        config.distanceFromScaleToNumber = 8;
        //指示视图属性设置
        config.pointSize = CGSizeMake(1.5, 30);
        config.pointColor = k1F1F1F;
        config.pointStart = 0;
        //文字属性
        config.numberFont = [UIFont systemFontOfSize:14];
        config.numberColor = k1F1F1F;
        //数字所在位置方向
        config.numberDirection = numberBottom;
        
        //取值范围
        config.max = 150;
        config.min = 30;
        //默认值
        config.defaultNumber = [XMUtil isBlankString:kUserModel.weight] ? 170 : kUserModel.weight.doubleValue;
        config.infiniteLoop = YES;
        config.offset = 1;
//        config.selectionEnable = YES;
        _wRulerView.rulerConfig = config;
    }
    return _wRulerView;
}


- (void)layoutSubviews {
    [super layoutSubviews];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                                   byRoundingCorners:(UIRectCornerTopRight | UIRectCornerTopLeft)
                                                         cornerRadii:CGSizeMake(20, 20)];
    
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}

@end
